#!/usr/bin/env python3
"""
简单的问题诊断脚本
"""
import sys
import os
from pathlib import Path

def main():
    """主诊断函数"""
    print("=== SmartCity-LLM Trainer 环境诊断 ===")
    print()
    
    # 检查Python版本
    print("1. Python版本检查:")
    print(f"   版本: {sys.version}")
    if sys.version_info < (3, 8):
        print("   ❌ Python版本过低，需要3.8+")
    else:
        print("   ✅ Python版本符合要求")
    print()
    
    # 检查关键Python包
    print("2. 关键包检查:")
    packages = ['fastapi', 'uvicorn', 'sqlalchemy', 'pydantic', 'aiosqlite']
    
    for package in packages:
        try:
            __import__(package)
            print(f"   ✅ {package}")
        except ImportError:
            print(f"   ❌ {package} 未安装")
    print()
    
    # 检查项目文件
    print("3. 项目文件检查:")
    backend_dir = Path(__file__).parent
    
    files = ['main.py', 'requirements.txt']
    for file_path in files:
        if (backend_dir / file_path).exists():
            print(f"   ✅ {file_path}")
        else:
            print(f"   ❌ {file_path} 缺失")
    print()
    
    # 尝试导入main模块
    print("4. 主模块导入测试:")
    try:
        if str(backend_dir) not in sys.path:
            sys.path.insert(0, str(backend_dir))
        
        import main
        print("   ✅ main模块导入成功")
        
        if hasattr(main, 'app'):
            print("   ✅ FastAPI app实例找到")
        else:
            print("   ❌ FastAPI app实例未找到")
            
    except Exception as e:
        print(f"   ❌ 导入失败: {e}")
        print("   详细错误信息:")
        import traceback
        traceback.print_exc()
    
    print()
    print("=== 诊断完成 ===")
    print()
    print("如果看到错误，建议:")
    print("1. 安装缺失的包: pip install fastapi uvicorn sqlalchemy pydantic aiosqlite python-jose passlib")
    print("2. 检查是否在正确的目录中运行")
    print("3. 确保所有项目文件都存在")
    print()
    
    input("按回车键退出...")

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(f"诊断过程出错: {e}")
        input("按回车键退出...")